<?php
declare(strict_types=1);

namespace App\Controller\v1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Service\UserService;
use App\Middleware\Api\AuthMiddleware;
use App\Service\PersonService;
use App\Service\ShowService;
use App\Service\SubService;
use App\Service\PlatformService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class HomeController extends AbstractController
{
    #[Inject]
    protected UserService $userServ;
    #[Inject]
    protected PersonService $psServ;
    #[Inject]
    protected ShowService $showServ;
    #[Inject]
    protected SubService $subServ;
    #[Inject]
    protected PlatformService $platServ;

    #获取人物
    #[RequestMapping(path: "person", methods: "post")]
    public function person(){
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $params = [
            'currentPage'=>$currentPage,
            'pageSize'=>$pageSize
        ];
        $res = $this->psServ->getPersonPage($params);
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #首页-演出列表
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "showlist", methods: "post")]
    public function showlist(){
        $person_id = $this->request->input("person_id", 0);
        $month = $this->request->input("month", "");
        $city_id = $this->request->input("city_id", 0);
        $sort = $this->request->input("sort", 0);
        $user_id = $this->request->getAttribute('user_id');
        if(empty($month)){
            return $this->fmt_response(ReturnCode::para_miss,'月份错误');
        }
        $params = [
            'user_id'=>$user_id,
            'person_id'=>$person_id,
            'month'=>$month,
            'city_id'=>$city_id,
            'sort'=>$sort
        ];
        $res = $this->showServ->getShowMonth($params);
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #首页-演出详情
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "showdetail", methods: "post")]
    public function showdetail(){
        $showdate_id = $this->request->input("showdate_id", 0);
        if(empty($showdate_id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $params = [
            'user_id'=>$user_id,
            'showdate_id'=>$showdate_id
        ];
        $res = $this->showServ->getShowDetail($params);
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #第三方平台详情
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "platform-detail", methods: "post")]
    public function platformDetail(){
        $platform_id = $this->request->input("platform_id", 0);
        if(empty($platform_id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $params = [
            'user_id'=>$user_id,
            'platform_id'=>$platform_id
        ];
        $res = $this->platServ->getDetail($params);
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #订阅详情
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "sub-detail", methods: "post")]
    public function subDetail(){
        $grabdate_id = $this->request->input("grabdate_id", 0);
        if(empty($grabdate_id)){
            return $this->fmt_response(ReturnCode::deal_succ, '获取成功', NULL);
        }
        $user_id = $this->request->getAttribute('user_id');
        $params = [
            'user_id'=>$user_id,
            'grabdate_id'=>$grabdate_id
        ];
        $res = $this->showServ->getSubDetail($params);
        if($res===false){
            return $this->fmt_response(ReturnCode::para_miss,'抢票时间已过期');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #票档详情
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "level-detail", methods: "post")]
    public function levelDetail(){
        $showdate_id = $this->request->input("showdate_id", 0);
        if(empty($showdate_id)){
            return $this->fmt_response(ReturnCode::deal_succ, '获取成功', NULL);
        }
        $user_id = $this->request->getAttribute('user_id');
        $params = [
            'user_id'=>$user_id,
            'showdate_id'=>$showdate_id
        ];
        $res = $this->showServ->getLevelDetail($params);
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #订阅通知
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "sub", methods: "post")]
    public function sub(){
        $grabdate_id = $this->request->input("grabdate_id", 0);
        $sub_time = $this->request->input("sub_time", "");
        $user_id = $this->request->getAttribute('user_id');
        if(empty($user_id)){
            return $this->fmt_response(ReturnCode::para_miss,'用户未登录');
        }
        if(empty($grabdate_id)){
            return $this->fmt_response(ReturnCode::para_miss,'抢票时间ID错误');
        }
        if(empty($sub_time)){
            return $this->fmt_response(ReturnCode::para_miss,'订阅提醒时间错误');
        }
        $grabtime = Db::table('vote_showdate_detail')
            ->where(['id'=>$grabdate_id,'is_del'=>0,'status'=>1])
            ->value("grabtime");
        if(strtotime($sub_time)<=time()){
            return $this->fmt_response(ReturnCode::para_miss,'订阅提醒时间早于当前时间');
        }
        if(strtotime($sub_time)>$grabtime){
            return $this->fmt_response(ReturnCode::para_miss,'订阅提醒时间晚于抢票时间');
        }
        $params = [
            'user_id'=>$user_id,
            'grabdate_id'=>$grabdate_id,
            'sub_time'=>$sub_time
        ];
        $res = $this->subServ->subSave($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #取消订阅通知
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "unsub", methods: "post")]
    public function unsub(){
        $grabdate_id = $this->request->input("grabdate_id", 0);
        $user_id = $this->request->getAttribute('user_id');
        if(empty($user_id)){
            return $this->fmt_response(ReturnCode::para_miss,'用户未登录');
        }
        if(empty($grabdate_id)){
            return $this->fmt_response(ReturnCode::para_miss,'抢票时间ID错误');
        }
        $params = [
            'user_id'=>$user_id,
            'grabdate_id'=>$grabdate_id
        ];
        $res = $this->subServ->unSubSave($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
}